#ifdef __cplusplus
extern "C" {
#endif



#include "Plugin.h"
#include "Application.h"
#include "__str.h"
#include "PluginDependence.h"

void __begheader(Plugin, initialize)
	PluginDescriptor * _tPluginDescriptor
__begbody
    Application * application = __instance(Application);
    __this(Plugin)->mName = (char *)malloc(MAX_PATH);
    ::memset(__this(Plugin)->mName, '\0', MAX_PATH);
	//::strcat(__this(Plugin)->mName, application->mSystemDirectory);
	//::strcat(__this(Plugin)->mName, "\\");
	::strcat(__this(Plugin)->mName, _tPluginDescriptor->mName);
__endbody


void __begvoid(Plugin, startup)
__begbody
    __this(Plugin)->mHandle = ::LoadLibraryA(__this(Plugin)->mName);

    __voidfun(__this(Plugin), Plugin, dependence)

	if (__this(Plugin)->mHandle != NULL)
	{
		typedef void (*Fun_t)();
		Fun_t run = (Fun_t)::GetProcAddress (__this(Plugin)->mHandle, "run");
		run();
	}
__endbody


void __begvoid(Plugin, dependence)
__begbody
    char * pluginName = (char *)malloc(MAX_PATH);
    ::memset(pluginName, '\0', MAX_PATH);
	::memcpy(pluginName, __this(Plugin)->mName, MAX_PATH);
	int pos = strRfindeChar(pluginName, '\\');
	for (int i = pos + 1; i < MAX_PATH; i++)
	{
		pluginName[i] = '\0';
	}
	::strcat(pluginName, "plugin.txt");

	PluginDependence pluginDependence;
	
	__begfun(&pluginDependence, PluginDependence, initialize)
		pluginName
	__endfun

	free(pluginName);
__endbody


#ifdef __cplusplus
}
#endif
